﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DoAnB
{
    public partial class qlHoaDon : Form
    {
        public qlHoaDon()
        {
            InitializeComponent();
        }

        private void load_ChiTietHD()
        {
            
            string connection = (@"Server = .\SQLEXPRESS; Integrated Security = True; Database = Amumu");
            SqlConnection cnn = new SqlConnection(connection);
            if (cnn.State == ConnectionState.Open)
                cnn.Close();
            try
            {
                cnn.Open();
                int MaHD = int.Parse(listBoxMaHD.SelectedValue.ToString());
                SqlCommand cmd = new SqlCommand("SELECT Ten, Gia, SoLuongDoDung FROM HoaDon, SanPham, ChiTietHoaDon, NhanVien WHERE HoaDon.MaHoaDon = " + MaHD +
                    "AND HoaDon.MaHoaDon = ChiTietHoaDon.MaHoaDon AND ChiTietHoaDon.MaSanPham = SanPham.ID AND NhanVien.MaNV = HoaDon.MaNV", cnn);

                SqlDataAdapter adap = new SqlDataAdapter(cmd);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adap);
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                adap.Fill(table);
                table.Columns["Ten"].ColumnName = "Tên Sản Phẩm";
                table.Columns["Gia"].ColumnName = "Giá Bán";
                table.Columns["SoLuongDoDung"].ColumnName = "Số Lượng";
                dgChiTietHD.DataSource = table;
                lblMaHD.Text = MaHD.ToString();

                SqlCommand com = new SqlCommand("Select dbo.NhanVien.MaNV, dbo.HoaDon.MaNV, TenNV, Ban, NgayBan, GiaTri FROM HoaDon, SanPham, ChiTietHoaDon, NhanVien WHERE HoaDon.MaHoaDon = " + MaHD +
                    "AND HoaDon.MaHoaDon = ChiTietHoaDon.MaHoaDon AND ChiTietHoaDon.MaSanPham = SanPham.ID AND dbo.NhanVien.MaNV = dbo.HoaDon.MaNV", cnn);
                SqlDataReader reader = com.ExecuteReader();

                try
                {
                    while (reader.Read())
                    {
                        lblMaNV.Text = reader["MaNV"].ToString();
                        lblTenNV.Text = (string)reader["TenNV"];
                        lblNgay.Text = ((DateTime)reader["NgayBan"]).ToString("yyyy-MM-dd");
                        lblGiaTri.Text = ((double)reader["GiaTri"]).ToString("###,###,##0");
                        lblBan.Text = (string)reader["Ban"];
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
                cnn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
        

        private void btXemHD_Click(object sender, EventArgs e)
        {
            ketnoi data = new ketnoi();
            string slq = "SELECT * FROM HoaDon WHERE NgayBan BETWEEN '" + dtPickerFrom.Value.ToString("yyyy-MM-dd") + "' AND '" + dtPickerTo.Value.ToString("yyyy-MM-dd") + "'";
            listBoxMaHD.DataSource = data.getDataTable(slq);
            listBoxMaHD.DisplayMember = "MaHoaDon";
            listBoxMaHD.ValueMember = "MaHoaDon";
            lblSoHD.Text = listBoxMaHD.Items.Count.ToString();
        }

        private void listBoxMaHD_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ChiTietHD();
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
